package com.lailai.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

/**
 * 拦截器
 *
 * @Author lgr
 * @Date 2025/6/27
 * @Wechat a983105834
 * @Email 983105834@qq.com
 * @Copyright <a href="www.lailai.ink">lailai个人空间</a>
 */
@Component
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 请求前执行（返回 false 则中断请求）
        System.out.println("请求开始: " + request.getRequestURI());

        // 示例：添加自定义头
        response.setHeader("X-App-Name", "LailaiApp");

        return true; // 继续执行后续逻辑
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 请求完成后执行（视图渲染前）
        System.out.println("请求处理完成: " + response.getStatus());
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 整个请求结束后执行（可用于资源清理）
        System.out.println("请求最终完成");
    }
}
